﻿using System.Diagnostics;
using System.IO;

namespace Expect.VisualStudio.Extensions.Commands.Handlers
{
    public class GenerateReportCommand : ReportConsoleCommand
    {
        public GenerateReportCommand(IExpectPackage pkg): base(pkg, CommandIDs.cmdidGenerateReport)
        {
            
        }

        protected override void AfterExecute()
        {
            if (Ask("Do you want to open the generated document ?"))
            {
                OpenDocument();
            }
        }

        protected override string GetCommandArguments()
        {
            return "-source=\"" + _source + "\"";
        }

        protected override string GetErrorMessage()
        {
            return
                "Expect could not generate the specification file. " + 
                "One common reason is having the document opened...\r\n" + 
                "Check the output tab to get detailed error.";
        }


        private void OpenDocument()
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = new ProcessStartInfo(_reportCfg.OutputFile);
            p.StartInfo.WorkingDirectory = _sourcedir;

            p.Start();
        }

        protected override bool Validate()
        {
            if (string.IsNullOrEmpty(_source) || !File.Exists(_source))
            {
                ShowMessage("Source assembly file \"" + _source + "\" not found, try building the project before starting this action !");
                return false;
            }

            if (string.IsNullOrEmpty(_reportAppPath) || !File.Exists(_reportAppPath))
            {
                ShowMessage("Report application not found");
                return false;
            }

            return true;
        }
    }
}
